package org.vectrics.recruiter.struts.admin;

import java.util.Iterator;
import java.util.List;

import org.apache.ecs.html.A;
import org.vectrics.SystemException;
import org.vectrics.common.html.BorderTable;
import org.vectrics.common.html.Cell;
import org.vectrics.common.html.Row;
import org.vectrics.common.html.Table;
import org.vectrics.provider.CoreServiceLocator;
import org.vectrics.recruiter.RecruitServiceLocator;
import org.vectrics.recruiter.job.Job;
import org.vectrics.recruiter.user.ResumeScanBatch;
import org.vectrics.recruiter.user.ResumeScanBatchJob;
import org.vectrics.struts.VectricsTagSupport;


public class JobCandidateScanManagerTag extends VectricsTagSupport {

	private static final long serialVersionUID = 70720601371540967L;
	
	private String jobId = null;

	public JobCandidateScanManagerTag() {
		super();
	}

	protected Job getJob() {
		return RecruitServiceLocator.getJobProvider().findJob(new Long(jobId));
	}
	protected String makeStartTag() {
		StringBuffer html = new StringBuffer();
		Job job = this.getJob();
		List currentScanBatchJobs = RecruitServiceLocator.getJobProvider().findResumeScanBatchJobs(job);
		
		Table scansTable = new BorderTable();
		scansTable.setWidth("100%");
		Row headerRow = scansTable.addRow();
        headerRow.setDefaultBgImage(this.getContextPath() + "/site-graphics/tbl_hdr.gif");
		//headerRow.setDefaultCellClass("tableHdrBg");
        headerRow.setDefaultCellFontClass("tableHdrFont");
        headerRow.addCell().addText(this.getPhrase("prompt.resume.scan.name"));
        headerRow.addCell().addText(this.getPhrase("prompt.resume.scan.data.run"));
        headerRow.addCell();
        headerRow.addCell();
	
        int numberOfColumns = headerRow.getColumnCount();
        
		Iterator iter = currentScanBatchJobs.iterator();
		if (iter.hasNext() == false) {
			Row row = scansTable.addRow();
			Cell cell = row.addCell();
			cell.setColSpan(numberOfColumns);
			cell.addText("subFont", "<i>"  + this.getPhrase("phrase.no.candidate.scans.for.job") + "</i>");
		}
		while (iter.hasNext()) {
			ResumeScanBatchJob resumeScanBatchJob = (ResumeScanBatchJob)iter.next();
			ResumeScanBatch resumeScanBatch = resumeScanBatchJob.getResumeScanBatch();
			Job currentJob = resumeScanBatchJob.getJob();
			if (currentJob.getId().equals(job.getId()) == false)
				throw new SystemException("Unexpected condition, job of scan batch does not match required job, something went wrong with the resumeScanBatchJob search");
			
			Row row = scansTable.addRow();
	        row.setDefaultCellFontClass("subFont");
			
			Cell cell = row.addCell();
			cell.addText(resumeScanBatch.getDescription());
			
			
			cell = row.addCell();
			if (resumeScanBatch.getRunDate() != null)
				cell.addText(CoreServiceLocator.getDateFormat()
					.getDateFormatRoundToMinute(this.getLocale()).format(resumeScanBatch.getRunDate()));
			
			cell = row.addCell();
			A viewCandidates = new A();
			viewCandidates.setHref(this.getContextPath() + "/admin/jobs/resumeScanResults.do?batchId=" + 
					resumeScanBatch.getId().toString()
					+ "&jobId=" + currentJob.getId().toString());
			viewCandidates.setClass("subLink");
			viewCandidates.addElement(this.getPhrase("prompt.scan.candidates.view"));
			cell.addElement(viewCandidates);
			
			cell = row.addCell();
			A runNowLink = new A();
			runNowLink.setHref(this.getContextPath() + "/admin/jobs/resumeScan.do?batchId=" + 
					resumeScanBatch.getId().toString());
			runNowLink.setClass("subLink");
			runNowLink.addElement(this.getPhrase("prompt.scan.run"));
			cell.addElement(runNowLink);
		}
		html.append(scansTable.toString());
		return html.toString();
	}

	public String getJobId() {
		return jobId;
	}

	public void setJobId(String jobId) {
		this.jobId = jobId;
	}

	

}
